package com.hms.dao.impl;

import java.util.List;

import org.springframework.stereotype.Component;

import com.hms.dao.ILocationDao;
import com.hms.entity.TdLocation;

@Component("locationDao")
public class LocationDao extends BaseDao<TdLocation, String> implements ILocationDao {

	@SuppressWarnings("unchecked")
	@Override
	public List<TdLocation> getRootLocationList() {
		String hql = " from TdLocation l where l.parent is null";
		return this.getSession().createQuery(hql).list();

	}

	@SuppressWarnings("unchecked")
	@Override
	public List<TdLocation> getParentLocationList(TdLocation childrenLocation) {
		TdLocation location = this.load(childrenLocation.getId());
		String[] ids = location.getPath().split(TdLocation.PATH_SEPARATOR);
		String hql = " from TdLocation l where l !=? and l.id in(:ids)";
		return this.getSession().createQuery(hql).setParameter(0, location).setParameterList("ids", ids).list();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<TdLocation> getAllChildrenLocationList(TdLocation parentLocation) {
		// String hql = " from TdLocation l where l.parent =?";
		// return this.getSession().createQuery(hql).setParameter(0,
		// parentLocation).list();
		String hql = "from TdLocation area where area != ? and area.path like ?";
		return getSession().createQuery(hql).setParameter(0, parentLocation).setParameter(1, parentLocation.getPath() + "%").list();
	}

	@Override
	public List<TdLocation> getAllImportCityList() {
		// String hql = " from TdLocation l where l.isImportantCity=1";
		return null;
	}

}
